 /* Name       : Sample Comm's Program - 1024 Byte Buffer - buff1024.c   */
 /* Written By : Craig Peacock <cpeacock@senet.com.au>                   */

 /*       Copyright 1997 CRAIG PEACOCK <cpeacock@senet.com.au>           */

 /*         See http://www.senet.com.au/~cpeacock/serial1.htm            */
 /*                       For More Information                           */



#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <curses.h>

#include "fake_int.h"

#define INTVECT 0x0C /* Com Port's IRQ here (Must also change PIC setting) */

/* Defines Serial Ports Base Address */
#define COM1 0x3F8
//#define COM2 0x2F8
// COM3 0x3E8			      
// COM4 0x2E8			       

#define PORT COM1

int bufferin = 0;
int bufferout = 0;
char ch;
char buffer[1025];

void (*oldport1isr)() ;


int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;
 
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
 
  ch = getchar();
 
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);
 
  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }
 
  return 0;
}


char inportb(unsigned int port)
{
	unsigned int result;
    __asm__ 
    (
		"movl  %[port],%%eax\n"
		"movw  %%ax,%%dx\n"
        "inb  %%dx,%%al\n"
        "movl %%eax,%[result]\n"
    :
		[result]"=r" (result)
	:
		[port] "r" (port)
	:"eax");	
	return (char)result;
}

void outportb(unsigned int port, unsigned int value)
{
    __asm__ 
    (
		"movl  %[port],%%eax\n"
		"movw  %%ax,%%dx\n"
		"movl  %[value],%%eax\n"
        "outb %%al,%%dx\n"
    :
	:
		[port] "r" (port),
		[value] "r" (value)
	:"eax","edx");	
}

void PORT1INT()  /* Interrupt Service Routine (ISR) for PORT1 */
{
 int c;
 do { c = inportb(PORT + 5);
      if (c & 1) {buffer[bufferin] = inportb(PORT);
		  bufferin++;
		  if (bufferin == 1024) {bufferin = 0;}}
    }while (c & 1);
 outportb(0x20,0x20);
}

int main(void)
{
 
 prep_fake_int();
 int c;
 outportb(PORT + 1 , 0);        /* Turn off interrupts - Port1 */
 printf("here0\n");
 oldport1isr = getvect(INTVECT); /* Save old Interrupt Vector of later recovery */

 printf("here\n");
 setvect(INTVECT, PORT1INT);     /* Set Interrupt Vector Entry */
				 /* COM1 - 0x0C */
				 /* COM2 - 0x0B */
				 /* COM3 - 0x0C */
				 /* COM4 - 0x0B */

 /*         PORT 1 - Communication Settings         */
 printf("here2\n");
 outportb(PORT + 3 , 0x80);  /* SET DLAB ON */
 outportb(PORT + 0 , 0x0C);  /* Set Baud rate - Divisor Latch Low Byte */
			      /* Default 0x03 =  38,400 BPS */
			      /*         0x01 = 115,200 BPS */
			      /*         0x02 =  57,600 BPS */
			      /*         0x06 =  19,200 BPS */
			      /*         0x0C =   9,600 BPS */
			      /*         0x18 =   4,800 BPS */
			      /*         0x30 =   2,400 BPS */
 outportb(PORT + 1 , 0x00);  /* Set Baud rate - Divisor Latch High Byte */
 outportb(PORT + 3 , 0x03);  /* 8 Bits, No Parity, 1 Stop Bit */
 outportb(PORT + 2 , 0xC7);  /* FIFO Control Register */
 outportb(PORT + 4 , 0x0B);  /* Turn on DTR, RTS, and OUT2 */

 outportb(0x21,(inportb(0x21) & 0xEF));  /* Set Programmable Interrupt Controller */
					 /* COM1 (IRQ4) - 0xEF  */
					 /* COM2 (IRQ3) - 0xF7  */
					 /* COM3 (IRQ4) - 0xEF  */
					 /* COM4 (IRQ3) - 0xF7  */

 outportb(PORT + 1 , 0x01);  /* Interrupt when data received */

 printf("\nSample Comm's Program. Press ESC to quit \n");

 do {

     if (bufferin != bufferout){ch = buffer[bufferout];
				bufferout++;
				if (bufferout == 1024) {bufferout = 0;}
				printf("%c",ch);}

     if (kbhit()){c = getchar();
		  outportb(PORT, c);}

    } while (c !=27);

 outportb(PORT + 1 , 0);       /* Turn off interrupts - Port1 */
 outportb(0x21,(inportb(0x21) | 0x10));  /* MASK IRQ using PIC */
					 /* COM1 (IRQ4) - 0x10  */
					 /* COM2 (IRQ3) - 0x08  */
					 /* COM3 (IRQ4) - 0x10  */
					 /* COM4 (IRQ3) - 0x08  */
 setvect(INTVECT, oldport1isr); /* Restore old interrupt vector */
	release_fake_int();
	return 0;
}

